# -*- coding: utf-8 -*-
# created on 2016/11/14

from sympy import simplify, expand
from sympy.abc import x, t
from sympy.core.add import Add
from sympy.core.function import UndefinedFunction
from sympy.core.power import Pow
from mathsolver.functions.base.base import BaseFunction, new_latex
from mathsolver.functions.base.objects import BaseFunc
from mathsolver.functions.sympy_utils import get_all_child


class GouChengFangChengZuQiuJieXiShi(BaseFunction):
    def solver(self, *args):
        """构成方程组求函数解析式"""
        left, right = args[0].sympify()
        funcs = get_all_child(left - right, lambda xx: isinstance(type(xx), UndefinedFunction))
        if len(funcs) != 2:
            return self
        f1, f2 = funcs
        is_product = True
        if '-' in str(f1.args) or '-' in str(f2.args):
            is_product = False
            if '-' in str(f1.args):
                f1, f2 = f2, f1
            a = f1.args[0].args[0] if len(f1.args[0].args) == 2 else 0
            b = f2.args[0].args[0] if isinstance(f2.args[0], Add) else 0
        else:
            if f1.has(Pow) and not f2.has(Pow):
                f1, f2 = f2, f1
            a = f1.args[0].args[0] if len(f1.args[0].args) == 2 else 1
            b = f2.args[0].args[0] if f2.args[0].args[1] == 1 / x else 1
        subs_expr_t = b / t if is_product else b - t
        subs_expr_x = b / x if is_product else b - x
        left = simplify(left.subs(x, subs_expr_t))
        right = expand(simplify(right.subs(x, subs_expr_t)))
        self.steps.append(
            ["令{}=t，即x={}，代入原方程得{}={}".format(new_latex(subs_expr_x), new_latex(subs_expr_t), new_latex(left), new_latex(right)), ""])
        left = left.subs(t, x)
        right = right.subs(t, x)
        self.steps.append(["即{}={}".format(new_latex(left), new_latex(right)), "①"])
        subs_expr = a * b / x if is_product else a + b - x
        left_subs = simplify(left.subs(x, subs_expr))
        right_subs = simplify(right.subs(x, subs_expr))
        self.steps.append(
            ["将{}代入上面方程，得{}={}".format(new_latex(subs_expr), new_latex(left_subs), new_latex(right_subs)), "②"])
        self.steps.append(["联立①②两个方程", ""])
        # 因为jiefangchenzu无法解决以f(x)为未知数的方程组，所以自己用消元的方法来解
        f1_subs, f2_subs = get_all_child(left_subs - right_subs, lambda xx: isinstance(type(xx), UndefinedFunction))
        if len(f1_subs.args) == 1 and f1_subs.args[0] == x:
            m, n = left.coeff(f1_subs), left.coeff(f2_subs)
            m_subs, n_subs = left_subs.coeff(f1_subs), left_subs.coeff(f2_subs)
            pass
        elif len(f2_subs.args) == 1 and f2_subs.args[0] == x:
            m, n = left.coeff(f2_subs), left.coeff(f1_subs)
            m_subs, n_subs = left_subs.coeff(f2_subs), left_subs.coeff(f1_subs)
            pass
        else:
            return self
        gx = (n_subs * right - n * right_subs) / (m * n_subs - m_subs * n)
        gx = expand(gx)
        op = '-'
        if n < 0:
            op = '+'
            n = -n
        self.steps.append(
            ["(" + str(n_subs) + "*①" + op + str(n) + "*②)/" + str(m * n_subs - m_subs * n),
             "得f(x)={}".format(new_latex(gx))])
        self.output.append(BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': gx}))
        self.label.add("构成方程组求函数解析式")
        return self


if __name__ == '__main__':
    pass
